/*
The MIT License (MIT)

Copyright (c) 2013 Sergey Nechaev <serg.nechaev@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */
package com.sergnechaev.web.combine.filter;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * 
 * @author Sergey Nechaev <serg.nechaev@gmail.com>
 *
 */
@SuppressWarnings("serial")
public final class Resource implements Serializable {

	public static final String UTF_8 = "UTF-8";

	private static final String HEADER_CACHE_CONTROL = "Cache-Control";
	private static final String HEADER_E_TAG = "ETag";
	private static final String HEADER_EXPIRES = "Expires";
	private static final String HEADER_LAST_MODIFIED = "Last-Modified";

	public String data;
	public int length;
	public long lastModified;
	public long expires;
	public String eTag;
	public String cacheControl;
	public String contentType;
	public byte[] byteData;

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	public void writeOk(HttpServletResponse resp) throws IOException {

		// dump
		resp.reset();

		// Response status
		resp.setStatus(HttpServletResponse.SC_OK);

		// Response length
		resp.setContentLength(length);

		// Last-Modified header (long time ago)
		resp.setDateHeader(HEADER_LAST_MODIFIED, lastModified);

		// Set 'forever cached' policy
		// Expires header (in 5 years)
		resp.setDateHeader(HEADER_EXPIRES, expires);

		// ETag headers
		resp.setHeader(HEADER_E_TAG, eTag);

		// Cache-Control header
		resp.setHeader(HEADER_CACHE_CONTROL, cacheControl);

		// Set up content type
		resp.setContentType(contentType);

		// Dump it
		if (data != null) {

			// Response encoding
			resp.setCharacterEncoding(UTF_8);

			IOUtils.write(data, resp.getOutputStream(), UTF_8);
		} else {
			IOUtils.write(byteData, resp.getOutputStream());
		}
	}

	public void writeNotModified(HttpServletResponse resp) throws IOException {

		// dump
		resp.reset();

		// Response status
		resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);

		if (data != null) {
			// Response encoding
			resp.setCharacterEncoding(UTF_8);
		}

		// Last-Modified header (long time ago)
		resp.setDateHeader(HEADER_LAST_MODIFIED, lastModified);

		// Set 'forever cached' policy
		// Expires header (in 5 years)
		resp.setDateHeader(HEADER_EXPIRES, expires);

		// ETag headers
		resp.setHeader(HEADER_E_TAG, eTag);

		// Cache-Control header
		resp.setHeader(HEADER_CACHE_CONTROL, cacheControl);

	}

}
